"""
GoodsListPage:商品列表页面
author:吕俊
"""
from Common.Base import Base, open_browser
from Page.AdminLoginPage import AdminLoginPage, login_url
import time


class GoodsListPage(Base):
    """
    定位器封装
    """
    goods_admin_button_loc = ('css selector', 'ul[id="menu-ul"]>li:nth-of-type(2)')
    class_loc = ('name', 'cat_id')
    brand_loc = ('name', 'brand_id')
    intro_type_loc = ('name', 'intro_type')  # 推荐类别定位器
    frame_loc = ('css selector', '#main-frame')
    check_loc = ('css selector', 'div.form-div > form > button')  # 搜索按钮定位器
    supplier_loc = ('css selector', 'body > div.form-div > form > select:nth-child(4)')  # 供货商定位器
    keyword_loc = ('css selector', 'body > div.form-div > form > input[type=text]:nth-child(7)')
    loc = ('xpath', r'//*[@id="totalRecords"]')
    del_goods_loc = ('xpath', '//*[@id="listDiv"]/table[1]/tbody/tr[3]/td[13]/a[4]')
    """
    定位器封装
    """

    def click_goods_admin_button(self):
        """
        点击<商品管理>按钮
        :return:
        """
        self.driver.switch_to.frame('menu-frame')
        self.find_element(self.goods_admin_button_loc).click()
        self.driver.switch_to.parent_frame()

    def select_goods_by_class(self, text):
        """
        通过类别搜索商品
        :param text:
        :return:
        """
        self.driver.switch_to.frame('main-frame')
        self.drop_down_list(self.class_loc, text)
        self.driver.switch_to.parent_frame()

    def select_goods_by_brand(self, text):
        """
        通过品牌搜索商品
        :param text:
        :return:
        """
        self.driver.switch_to.frame('main-frame')
        self.drop_down_list(self.brand_loc, text)
        self.driver.switch_to.parent_frame()

    def select_goods_by_intro_type(self, text):
        """
        通过推荐类别搜索商品
        :param text:
        :return:
        """
        self.driver.switch_to.frame('main-frame')
        self.drop_down_list(self.intro_type_loc, text)
        self.driver.switch_to.parent_frame()

    def select_goods_by_supplier(self, text):
        """
        通过供货商搜索商品
        :return:
        """
        self.driver.switch_to.frame('main-frame')
        self.drop_down_list(self.supplier_loc, text)
        self.driver.switch_to.parent_frame()

    def select_goods_by_keyword(self, text: str):
        """
        通过关键字搜索商品
        :return:
        """
        self.driver.switch_to.frame('main-frame')
        self.send_keys(self.keyword_loc, text)
        self.driver.switch_to.parent_frame()

    def check_button(self):
        """
        点击搜索按钮
        :return:
        """
        self.driver.switch_to.frame('main-frame')
        self.click(self.check_loc)
        self.driver.switch_to.parent_frame()

    def del_new_add_goods(self):
        self.driver.switch_to.frame('main-frame')
        self.click(self.del_goods_loc)
        self.switch_alert('accept')
        self.driver.switch_to.parent_frame()


    def assert_select(self, element_loc=loc):
        """
        断言条件搜索功能
        :return:
        """
        # loc = ('css locator', '#listDiv>table:nth-child(1)>tbody>tr:nth-child(3)>td.first-cell>span')
        self.driver.switch_to.frame('main-frame')
        time.sleep(4)
        element = self.get_element_text(element_loc)
        self.driver.switch_to.parent_frame()
        return element


# //*[@id="listDiv"]/table[1]/tbody/tr[3]/td[3]/span
# //*[@id="listDiv"]/table[1]/tbody/tr[3]/td[3]/span


if __name__ == '__main__':
    driver = open_browser()
    admin_login = AdminLoginPage(driver)
    admin_login.go_url(login_url)
    select_goods = GoodsListPage(driver)
    select_goods.click_goods_admin_button()
    # num2 = select_goods.assert_select()
    # element1 = select_goods.assert_select()
    time.sleep(2)
    select_goods.select_goods_by_class('手机')
    select_goods.check_button()
    time.sleep(5)
    element2 = select_goods.assert_select()
    # print(element1)
    print(element2)
    select_goods.click_goods_admin_button()
    select_goods.select_goods_by_brand('仓品')
    select_goods.check_button()
    element3 = select_goods.assert_select()
    print(element3)
    select_goods.click_goods_admin_button()
    select_goods.select_goods_by_supplier('上海供货商')
    select_goods.check_button()
    element4 = select_goods.assert_select()
    print(element4)
    # select_goods.back_browser()
    # select_goods.click_goods_admin_button()
    # select_goods.close_browser()
